package com.tony.business.util.curator;

import com.tony.business.AccessControlApp;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.stereotype.Component;

@Component
public class CuratorLock {

    public enum PathEnum {
        LOCK("/lock/");

        private String path;

        PathEnum(String path) {
            this.path = path;
        }

        public String getPath() {
            return this.path;
        }

    }

    public static InterProcessMutex getLock(Integer id, PathEnum pathEnum) {
        CuratorUtil curatorUtil = AccessControlApp.getBean(CuratorUtil.class);
        String lockpath = pathEnum.getPath() + id.toString();
        return new InterProcessMutex(curatorUtil.getClient(), lockpath);
    }
}